package com.mlethe.library.utils.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

/**
 * SharedPreferences工具类
 *
 * @author Mlethe
 * @date 2020/9/2
 */
public class SharedPreferencesUtil {

    private SharedPreferences.Editor edit;
    private SharedPreferences prefs;

    protected SharedPreferencesUtil() {
    }

    private final static class Holder {
        private static final SharedPreferencesUtil INSTANCE = new SharedPreferencesUtil();
    }

    protected static SharedPreferencesUtil getInstance() {
        return Holder.INSTANCE;
    }

    protected static SharedPreferences getPrefs() {
        return getInstance().prefs;
    }

    protected static SharedPreferences.Editor getEditor() {
        return getInstance().edit;
    }

    /**
     * 初始化
     *
     * @param context 上下文
     * @param name    文件名称
     */
    @SuppressLint("CommitPrefEdits")
    public static void init(Context context, String name) {
        getInstance().prefs = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        getInstance().edit = getInstance().prefs.edit();
    }

    /**
     * * 设置值
     *
     * @param key   键
     * @param value 值
     */
    public static boolean setValue(String key, Object value) {
        SharedPreferences.Editor edit = getEditor();
        if (edit == null) {
            throw new IllegalArgumentException("SharedPreferences.Editor Not initialized!");
        }
        if (value == null) {
            return edit.remove(key).commit();
        } else if (value instanceof Boolean) {
            return edit.putBoolean(key, (Boolean) value).commit();
        } else if (value instanceof Long) {
            return edit.putLong(key, (Long) value).commit();
        } else if (value instanceof Integer) {
            return edit.putInt(key, (Integer) value).commit();
        } else if (value instanceof Float) {
            return edit.putFloat(key, (Float) value).commit();
        } else if (value instanceof String) {
            return edit.putString(key, (String) value).commit();
        } else if (value instanceof Set) {
            // HashSet<String>
            return edit.putStringSet(key, (Set<String>) value).commit();
        } else {
            throw new IllegalArgumentException("SharedPreferences can't be get this type: " + value.getClass().getName());
        }
    }

    /**
     * 设置值
     *
     * @param key   键
     * @param value 值
     */
    public static void applyValue(String key, Object value) {
        SharedPreferences.Editor edit = getEditor();
        if (edit == null) {
            throw new IllegalArgumentException("SharedPreferences.Editor Not initialized!");
        }
        if (value == null) {
            edit.remove(key).apply();
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (Boolean) value).apply();
        } else if (value instanceof Long) {
            edit.putLong(key, (Long) value).apply();
        } else if (value instanceof Integer) {
            edit.putInt(key, (Integer) value).apply();
        } else if (value instanceof Float) {
            edit.putFloat(key, (Float) value).apply();
        } else if (value instanceof String) {
            edit.putString(key, (String) value).apply();
        } else if (value instanceof Set) {
            // HashSet<String>
            edit.putStringSet(key, (Set<String>) value).apply();
        } else {
            throw new IllegalArgumentException("SharedPreferences can't be get this type: " + value.getClass().getName());
        }
    }

    /**
     * 获取值
     *
     * @param key      键
     * @param defValue 默认值
     * @param <T>
     * @return
     */
    public static <T> T getValue(String key, T defValue) {
        SharedPreferences prefs = getPrefs();
        if (prefs == null) {
            throw new IllegalArgumentException("SharedPreferences Not initialized!");
        }
        if (defValue instanceof Boolean) {
            return (T) Boolean.valueOf(prefs.getBoolean(key, (Boolean) defValue));
        } else if (defValue instanceof Long) {
            return (T) Long.valueOf(prefs.getLong(key, (Long) defValue));
        } else if (defValue instanceof Integer) {
            return (T) Integer.valueOf(prefs.getInt(key, (Integer) defValue));
        } else if (defValue instanceof Float) {
            return (T) Float.valueOf(prefs.getFloat(key, (Float) defValue));
        } else if (defValue instanceof String) {
            return (T) prefs.getString(key, (String) defValue);
        } else if (defValue instanceof Set) {
            // HashSet<String>
            return (T) prefs.getStringSet(key, (Set<String>) defValue);
        }
        throw new IllegalArgumentException("SharedPreferences can't be get this type: " + defValue.getClass().getName());
    }

    /**
     * 删除值
     */
    public static void remove(String key) {
        getEditor().remove(key).apply();
    }

    /**
     * 清除缓存
     */
    public static void clear() {
        getEditor().clear().apply();
    }
}
